import Vue from "vue";
import VueRouter from "vue-router";
import { encryptByDES, decryptByDES } from "../utils/des";

Vue.use(VueRouter);

const getQueryVariable = (code, variable) => {
  if (!code) return;
  var query = code;
  var vars = query.split("&");
  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
  return false;
};

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/user/login')
  },
  {
    path: '/',
    redirect: '/moment',
    component: () => import('@/components/footer'),
    children: [
      {
        path: 'moment',//首页是默认子路由，所谓为空
        name: 'moment',
        component: () => import('@/views/Moments/')
      },
      {
        path: 'message',
        name: 'message',
        component: () => import('@/views/Message/'),
      },
      {
        path: 'advice',
        name: 'advice',
        component: () => import('@/views/Advice/')
      },
      {
        path: 'cart',
        name: 'cart',
        component: () => import('@/views/Mine/Cart/')
      },
      {
        path: 'mine',
        name: 'mine',
        component: () => import('@/views/Mine/'),
      }
    ]
  },
  {
    path: '/release',
    name: 'release',
    component: () => import('@/views/Release'),
  },
  {
    path: '/search',
    name: 'search',
    component: () => import('@/views/Moments/Search'),
  },
  {
    path: '/notice_detail',
    name: 'notice_detail',
    component: () => import('@/views/Notice/Detail'),
  },
  {
    path: '/prefer',
    name: 'prefer',
    component: () => import('@/views/Mine/Prefer')
  },
  {
    path: '/password',
    name: 'password',
    component: () => import('@/views/Mine/Password')
  },
  {
    path: '/setAddress',
    name: 'setAddress',
    component: () => import('@/views/Mine/setAddress')
  },
  {
    path: '/myinfo',
    name: 'myinfo',
    component: () => import('@/views/Mine/myInfo/')
  },
  {
    path: '/notice',
    name: 'notice',
    component: () => import('@/views/Notice/'),
  },
  {
    path: '/certify',
    name: 'certify',
    component: () => import('@/views/Mine/certify/')
  },
  {
    path: '/settle',
    name: 'settle',
    component: () => import('@/views/Mine/settle/')
  },
  {
    path: '/myOrder',
    name: 'myOrder',
    component: () => import('@/views/Mine/myOrder/')
  },
  {
    path: '/chatroom',
    name: 'chatroom',
    component: () => import('@/views/Message/room/')
  },
]



const router = new VueRouter({
  mode: "history",
  routes
});

//导航守卫(未登录情况下不能通过路修改路由直接查看)
router.beforeEach((to, from, next) => {
  let token = localStorage.getItem("token")
  if (token || to.path === '/login') {
    next()
  } else {
    next('/login')
  }
})
export default router;
